package com.example.multiple.config;

import com.example.multiple.properties.AllDatasourceProperties;
import com.example.multiple.properties.DatasourceProperties;
import com.example.multiple.properties.DefaultDataSourceProperties;
import com.example.multiple.properties.Test1DataSourceProperties;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yanggld
 * @Date 2019/6/19-11:14
 * @Description
 */
@Slf4j
@Configuration
@MapperScan(basePackages="com.example.multiple.mapper")
public class MybatisConfig {

	@Autowired
	private Environment environment;

	@Autowired
	private DefaultDataSourceProperties defaultDataSourceProperties;

	@Autowired
	private Test1DataSourceProperties test1DataSourceProperties;



//	/**
//	 * 默认使用的数据源
//	 * @throws Exception
//	 */
//	@Bean(name="dataSourceFTest1")
//	public DataSource defaultDataSource(){
//		HikariDataSource dataSource = new HikariDataSource();
//		dataSource.setDriverClassName(defaultDataSourceProperties.getDriverClassName());
//		dataSource.setJdbcUrl(defaultDataSourceProperties.getUrl());
//		dataSource.setUsername(defaultDataSourceProperties.getUsername());
//		dataSource.setPassword(defaultDataSourceProperties.getPassword());
//		return dataSource;
//	}
//
//	/**
//	 * 创建 回归环境 dataSource
//	 * @throws Exception
//	 */
//	@Bean(name="dataSourceFTest1")
//	public DataSource dataSourceFTest1(){
//		HikariDataSource dataSource = new HikariDataSource();
//		dataSource.setDriverClassName(test1DataSourceProperties.getDriverClassName());
//		dataSource.setJdbcUrl(test1DataSourceProperties.getUrl());
//		dataSource.setUsername(test1DataSourceProperties.getUsername());
//		dataSource.setPassword(test1DataSourceProperties.getPassword());
//		return dataSource;
//	}

	@Autowired
	private MyDataSourceConfig myDataSourceConfig;


	/**
	 * 创建动态数据源
	 */
	@Bean(name="dynamicDataSource")
	public DynamicDataSource dynamicDataSource(){
		Map<Object, Object> targetDataSource = new HashMap<>();
		List<MyDataSource> myDataSourceList = myDataSourceConfig.getDs();
		for (MyDataSource ds : myDataSourceList) {
			targetDataSource.put(ds.getNickname(), ds);
		}
		//获取动态数据库的实例（单例方式）
		DynamicDataSource dynamicDataSource = DynamicDataSource.getInstance();
		dynamicDataSource.setTargetDataSources(targetDataSource);
		//  设置默认的数据源
		dynamicDataSource.setDefaultTargetDataSource(myDataSourceList.get(0));
		return dynamicDataSource;
	}


	/**
	 * 2、根据数据源创建SqlSessionFactory
	 * @throws Exception
	 */
	@Bean(name="sessionFactory")
	public SqlSessionFactoryBean  sqlSessionFactoryBean(){
		SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
		sessionFactoryBean.setDataSource(dynamicDataSource());
		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		//此处设置为了解决找不到mapper文件的问题
		String property = environment.getProperty("mybatis.mapperLocations");
		try {
			Resource[] resources = resolver.getResources(property);
			sessionFactoryBean.setMapperLocations(resources);
		} catch (IOException e) {
			log.info("xml文件未找到:{}",e.getMessage());
		}
		return sessionFactoryBean;
	}
}